package com.wteam.controller;

import com.wteam.PoJo.DTO.goods;
import com.wteam.PoJo.DTO.goods_type;
import com.wteam.PoJo.VO.pageGoods;
import com.wteam.PoJo.VO.shopingvo;
import com.wteam.service.GoodsService;
import com.wteam.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "商品接口")
@RequestMapping("/goods")

public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     *
     * @param goodsType
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "注册商品类型")
    @PostMapping("/type")
    @CacheEvict(value = "goods_type", allEntries = true)
    public Result regist(@RequestBody @Valid goods_type goodsType) throws Exception {
        return goodsService.addGoodsType(goodsType);
    }

    /**
     *
     * @param shopingvo
     * @return
     */
    @ApiOperation(value = "发布商品")
    @PostMapping("/save")
    @CacheEvict(value = "goods_", allEntries = true)
    public Result savegoods(@RequestBody @Valid shopingvo shopingvo) {
        log.info(shopingvo.toString());
        return goodsService.saveGoods(shopingvo);
    }

    /**
     *
     * @return
     */
    @ApiOperation(value = "查看商品类型")
    @GetMapping("/typelist")
    @Cacheable(value = "goods_type")
    public Result <List<goods_type>>listtype() {
        List<goods_type> goods_types = goodsService.select();
        return Result.success(goods_types);
    }

    /**
     *
     * @param pageGoods
     * @return
     */
    @ApiOperation(value = "查看商品")
    @PostMapping("/listgoods")
    @Cacheable(value = "goods_", key = "#pageGoods.toString()")
    public Result<List<goods>> listgoods(@RequestBody pageGoods pageGoods) {
        List<goods> goods = goodsService.pagelistgoods(pageGoods);
        return Result.success(goods);
    }


    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "收藏商品")
    @GetMapping("/collectgoods/{id}")

    public Result SaveCollectGoods(@PathVariable Integer id) {
        return goodsService.SaveCollectGoods(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除商品收藏")
    @ApiImplicitParam(name = "ids", value = "商品收藏ID列表")
    @DeleteMapping("/deletecollectgoods/{ids}")

    public Result deleteCollectGoods(@PathVariable List<Integer>ids) {
        return goodsService.deleteCollectGoods(ids);
    }

    /**
     *
     * @param page
     * @param pagesize
     * @return
     */
    @ApiOperation(value = "查询收藏商品")
    @GetMapping("/listcollectgoods/{page}/{pagesize}")
//    @Cacheable(value = "goods_collect", key = "#page+'_'+#pagesize")
    public Result <List<goods>>listCollectGoods(@PathVariable Integer page, @PathVariable Integer pagesize) {
        return goodsService.listCollectGoods(page, pagesize);
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个商品")
    @GetMapping("/onegoods/{id}")
    public Result <goods>OneGoods(@PathVariable @ApiParam("商品id") Integer id) {
        return goodsService.OneGoods(id);
    }


}
